package com.pybox.spec;

import com.pybox.entity.TOptionLogs;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.util.StringUtils;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import java.util.*;


/**
* 描述：TOptionLogs Specification
* @author wuyingkai
* @date 2018-09-15
*/


public class TOptionLogsSpec implements Specification<TOptionLogs> {

    private Map<String, String> params;

    public TOptionLogsSpec(Map<String, String> params){
        this.params = params;
    }

    @Override
    public Predicate toPredicate(Root<TOptionLogs> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {


        String adminId = this.params.get("adminId");
        String event = this.params.get("event");
        String ctime = this.params.get("ctime");
        String utime = this.params.get("utime");
        List<Predicate> ps = new ArrayList<>();


        if (!StringUtils.isEmpty(adminId)) {
            ps.add(criteriaBuilder.like(root.get("adminId"), "%" + adminId + "%"));
        }
        if (!StringUtils.isEmpty(event)) {
            ps.add(criteriaBuilder.like(root.get("event"), "%" + event + "%"));
        }
        if (!StringUtils.isEmpty(ctime)) {
            ps.add(criteriaBuilder.like(root.get("ctime"), "%" + ctime + "%"));
        }
        if (!StringUtils.isEmpty(utime)) {
            ps.add(criteriaBuilder.like(root.get("utime"), "%" + utime + "%"));
        }

        criteriaQuery = criteriaQuery.where(ps.toArray(new Predicate[ps.size()]));

        return criteriaQuery.getRestriction();
    }

}